// Vue
import Vue from 'vue';
import VueRouter from 'vue-router';
import Vuex from 'vuex';
import axios from 'axios';
import ElementUI from 'element-ui'

Vue.use(VueRouter);
Vue.use(Vuex);
Vue.use(ElementUI, { size: 'small' });

// Services
import AuthorizeService from 'services/authorize.service.js';
import DataService from 'services/data.service.js';
import ConfigService from 'services/config.service';
import PriceService from 'services/price.service';
import DashboardService from 'views/dashboard/dashboard.service.js';
import PaymentService from 'views/payment/payment.service.js';
import PassportService from 'views/passport/passport.service.js';
import CompeteService from 'views/compete/compete.service.js';
import ShowService from 'views/show/show.service.js';
import TicketService from 'views/ticket/ticket.service.js';
import SessionService from 'views/session/session.service';
import LogService from 'views/log/log.service';
import TradeAuditService from 'views/trade_audit/trade_audit.service';
import HotelService from 'views/hotel/hotel/hotel.service';
import MealService from 'views/hotel/meal/meal.service';

Vue.use(AuthorizeService);
Vue.use(DataService);
Vue.use(DashboardService);
Vue.use(PaymentService);
Vue.use(PassportService);
Vue.use(CompeteService);
Vue.use(ShowService);
Vue.use(TicketService);
Vue.use(SessionService);
Vue.use(LogService);
Vue.use(TradeAuditService);
Vue.use(ConfigService);
Vue.use(PriceService);
Vue.use(HotelService);
Vue.use(MealService);

// Define stores
const store = new Vuex.Store({
    state: {
        unauditedPaymentsCount: 0, // 未审核支付单数量
        curPage: null
    },
    mutations: {
        // 修改未审核支付单数量
        unauditedPaymentsCountChanged(state, count) {
            state.unauditedPaymentsCount = count;
        },

        // 设置当前打开的页面
        setCurPage(state, page) {
            state.curPage = page;
        }
    }
});

// Define routers
import Routers from './router';
const router = new VueRouter({
    routes: Routers
});
router.beforeEach((to, from, next) => {
    if (to.name !== 'login') {
        if (!Vue.$AuthorizeService.hasLogin()) {
            router.replace({ name: 'login' });
        }
    }

    // 设置当前页面
    store.commit('setCurPage', to.name);

    next();
});

const vm = new Vue({
    el: '#app',
    router: router,
    store: store,
    components: {

    }
});

// Set axios global config
// axios.defaults.baseURL = aphf.cfg.ADMIN_API_ADDR;
axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8';

// Request
const re = /\\*admin/;
axios.interceptors
    .request.use(function(request) {
        // if (re.test(request.url)) {
        //     request.url = aphf.cfg.ADMIN_API_ADDR + request.url;
        // } else {
        //     request.url = aphf.cfg.API_ADDR + request.url;
        // }
        request.url = aphf.cfg.ADMIN_API_ADDR + request.url;
        return request;
    });

// Response
axios.interceptors
    .response.use(function(response) {
        if (response.data !== undefined && response.data !== null) {
            return Promise.resolve(response.data);
        }
        return response;
    }, function(error) {
        // if (error.response) {
        //     if (error.response.status === 401) {

        //     } else if (error.response.status === 404) {

        //     } else if (error.response.status === 500) {

        //     } else {
        //         router.replace({ name: '500' });
        //     }
        // } else {
        //     router.replace({ name: '500' });
        // }
        return Promise.reject(error);
    });


// 注册全局变量
Vue.prototype.cfg = {
    aphf_img_baseurl: aphf.img.ADDR
}

export {
    vm
}